package com.yjd.bi.demo;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;

import org.apache.tomcat.jdbc.pool.DataSource;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;

import com.yjd.bi.report.model.DataChangeRecordModel;
import com.yjd.bi.report.util.BiUtil;
import com.yjd.comm.base.concurrent.task.ITaskHandler;
import com.yjd.comm.base.concurrent.task.ITaskItem;
import com.yjd.comm.base.concurrent.task.TaskDisposeUtil;
import com.yjd.comm.util.FrameUtil;

public class Demo {

	public static void main(String[] args) throws SQLException,
			InterruptedException {
		f2();
	}

	public static void f3() throws SQLException {
		System.out.println(System.currentTimeMillis());
		DataSource dataSource = new DataSource();
		dataSource.setDriverClassName("com.mysql.jdbc.Driver");
		dataSource
				.setUrl("jdbc:mysql://192.168.11.13:3306/www_yijiedai_com?characterEncoding=UTF-8");
		dataSource.setUsername("www_yijiedai_com");
		dataSource.setPassword("yijiedai123");
		dataSource.setInitialSize(5);
		dataSource.setMaxActive(300);
		dataSource.setMinIdle(5);
		dataSource.setMaxIdle(20);
		dataSource.setTestOnBorrow(true);
		dataSource.setTestOnReturn(false);
		dataSource.setTestWhileIdle(true);
		dataSource.setValidationQuery("select 1");
		dataSource.setMaxWait(5000);
		dataSource.setTimeBetweenEvictionRunsMillis(600000);
		dataSource.setMinEvictableIdleTimeMillis(1800000);
		System.out.println(System.currentTimeMillis());
		JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
		System.out.println(BiUtil.getColumnNames("yjd_borrow", jdbcTemplate));
	}

	public static void f2() throws SQLException {
		System.out.println(System.currentTimeMillis());
		DataSource dataSource = new DataSource();
		dataSource.setDriverClassName("com.mysql.jdbc.Driver");
		dataSource
				.setUrl("jdbc:mysql://192.168.11.13:3306/www_yijiedai_com?characterEncoding=UTF-8&tinyInt1isBit=false");
		dataSource.setUsername("www_yijiedai_com");
		dataSource.setPassword("yijiedai123");
		dataSource.setInitialSize(5);
		dataSource.setMaxActive(300);
		dataSource.setMinIdle(5);
		dataSource.setMaxIdle(20);
		dataSource.setTestOnBorrow(true);
		dataSource.setTestOnReturn(false);
		dataSource.setTestWhileIdle(true);
		dataSource.setValidationQuery("select 1");
		dataSource.setMaxWait(5000);
		dataSource.setTimeBetweenEvictionRunsMillis(600000);
		dataSource.setMinEvictableIdleTimeMillis(1800000);
		System.out.println(System.currentTimeMillis());
		JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
		System.out.println(System.currentTimeMillis());
		List list = jdbcTemplate
				.queryForList("select * from yjd_borrow limit 10");
		System.out.println(jdbcTemplate
				.queryForMap("select * from yjd_borrow where status=? limit 1",1));
		System.out.println(System.currentTimeMillis());
		list = jdbcTemplate.queryForList("select * from yjd_cash limit 1");
		System.out.println(System.currentTimeMillis());
		for (Object object : list) {
			Map<String,Object> map = (Map<String, Object>) object;
			System.out.println(map.get("status").getClass());
		}
		System.out.println(FrameUtil.json(list,true));
	}

	public static void f1() throws InterruptedException {
		DataSource dataSource = new DataSource();
		dataSource.setDriverClassName("com.mysql.jdbc.Driver");
		dataSource
				.setUrl("jdbc:mysql://localhost:3306/test?characterEncoding=UTF-8");
		dataSource.setUsername("root");
		dataSource.setPassword("root");
		dataSource.setInitialSize(5);
		dataSource.setMaxActive(300);
		dataSource.setMinIdle(5);
		dataSource.setMaxIdle(20);
		dataSource.setTestOnBorrow(true);
		dataSource.setTestOnReturn(false);
		dataSource.setTestWhileIdle(true);
		dataSource.setValidationQuery("select 1");
		dataSource.setMaxWait(5000);
		dataSource.setTimeBetweenEvictionRunsMillis(600000);
		dataSource.setMinEvictableIdleTimeMillis(1800000);
		System.out.println(System.currentTimeMillis());
		final JdbcTemplate jt = new JdbcTemplate(dataSource);
		for (int i = 0; i < 1000; i++) {
			try {
				TaskDisposeUtil.dispose(true, 5,
						FrameUtil.newArrayList(1, 2, 3, 4),
						new ITaskHandler<Integer>() {

							@Override
							public void dispose(ITaskItem<Integer> item) {
								String sql = "select * from yjd_data_change_record order by id desc";
								List<DataChangeRecordModel> list = jt.query(
										sql,
										new RowMapper<DataChangeRecordModel>() {
											@Override
											public DataChangeRecordModel mapRow(
													ResultSet rs, int arg1)
													throws SQLException {
												DataChangeRecordModel m = new DataChangeRecordModel(
														rs.getLong("id"),
														rs.getString("database_code"),
														rs.getString("table_name"),
														rs.getString("ref_id"),
														rs.getInt("type"),
														rs.getLong("create_time"),
														rs.getInt("status"),
														rs.getLong("actual_dispose_time"),
														rs.getLong("version"));
												return m;
											}
										});
								System.out.println(list.size());

							}
						});

			} catch (Exception e) {
				System.out.println(e.getMessage());
			}
			Thread.sleep(1000);
		}
	}
}
